﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Sunny.UI;
using BaseDll;
using CommonTools;
using MotionIoLib;
using System.Threading;

namespace StationDemo
{
    public partial class Form_Manual锁附 : UIPage
    {
        string strStationName = "";
        public Form_Manual锁附(string strName)
        {
            strStationName = strName;
            InitializeComponent();
        }
        private void BtnEnable(bool b_State)
        {
            gb_Lock.Enabled = b_State;
            gb_StepRun.Enabled = b_State;
            gb_锁附测试.Enabled = b_State;
        }

        private void ChangeStep(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            string[] str_Value = btn.Name.Split('_');
            int nStep = Convert.ToInt32(str_Value[1]);
            SetTestStep(nStep);
        }

        private void SetTestStep(int nStep)
        {
            if (nStep == int.MaxValue)
            {
                nStep = gb_Lock.Controls.Count - 1;
            }
            foreach (var item in gb_Lock.Controls)
            {
                if (item.GetType() == typeof(Button))
                {
                    Button btn = (Button)item;
                    string[] str_Value = btn.Name.Split('_');
                    if (nStep == Convert.ToInt32(str_Value[1]))
                        btn.BackColor = Color.Green;
                    else
                        btn.BackColor = Color.Cyan;
                }
            }
            return;
        }

        private int GetTestStep(ref string str_Step)
        {
            int count = -1;
            foreach (var item in gb_Lock.Controls)
            {
                if (item.GetType() == typeof(Button))
                {
                    Button btn = (Button)item;
                    if (btn.BackColor == Color.Green)
                    {
                        string[] str_Value = btn.Name.Split('_');
                        str_Step = btn.Text;
                        count = Convert.ToInt32(str_Value[1]);
                        break;
                    }
                }
            }
            return count;
        }

        private async void RunOneStep_Click(object sender, EventArgs e)
        {
            DoWhile.ResetCirculate();
            Station_锁付站 station_锁付站 = (Station_锁付站)StationMgr.GetInstance().GetStation(strStationName);
            if (!UserDefineFunction.Check_AllAxis_回零())
            {
                return;
            }

            //运行该步骤
            string strResult = "";
            int n_执行步骤 = GetTestStep(ref strResult);
            if (n_执行步骤 == -1)
            {
                MessageBox.Show("步骤异常，请关闭软件重新初始化！", "Warn", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1, MessageBoxOptions.DefaultDesktopOnly);
                return;
            }
            BtnEnable(false);
            station_锁付站.Function_Z轴回安全位(true);
            try
            {
                LineStation2 line = (LineStation2)StationMgr.GetInstance().GetStation(Station.皮带线.ToString());
                await Task.Run(() =>
                {

                    switch (n_执行步骤)
                    {
                        case 0://回初始位
                            {
                                station_锁付站.Function_Z轴回安全位(true);
                                line.lineScrew.StopClyliner.Function_前进(true);
                                MessageBox.Show("锁付皮带线顶升气缸准备上升，请检查治具是否放好");
                                line.lineScrew.JackUpClyliner.Function_前进(true);
                                break;
                            }
                        case 1:
                            //取sensor板板
                            {
                                if (!station_锁付站.Function_取sensor板(true))
                                {
                                    n_执行步骤 = -99;
                                }
                                break;
                            }
                        case 2:
                            //定位sensor板板
                            {
                                if (!station_锁付站.Function_定位sensor板(true))
                                {
                                    n_执行步骤 = -99;
                                }
                                break;
                            }
                        case 3:
                            //取螺丝
                            {
                                if (!station_锁付站.Function_取螺丝(true))
                                {
                                    n_执行步骤 = -99;
                                }
                                break;
                            }
             
                        case 4:
                            //预备组装
                            {
                                if (!station_锁付站.Function_预备组装(true))
                                {
                                    n_执行步骤 = -99;
                                }
                                break;
                            }
                        case 5:
                            //定位前壳槽位
                            {
                                if (!station_锁付站.Function_定位前壳槽位(true))
                                {
                                    n_执行步骤 = -99;
                                }
                                break;
                            }
                        case 6:
                            //组装sensor板板
                            {
                                if (!station_锁付站.Function_放置sensor板(true))
                                {
                                    n_执行步骤 = -99;
                                }
                                break;
                            }
                        case 7:
                            //打螺丝1
                            {
                                if (!station_锁付站.Function_打螺丝(1, true))
                                {
                                    n_执行步骤 = -99;
                                }
                                break;
                            }
                        case 8:
                            //取螺丝
                            {
                                if (!station_锁付站.Function_取螺丝(true))
                                {
                                    n_执行步骤 = -99;
                                }
                                break;
                            }
                        case 9:
                            //打螺丝2
                            {
                                if (!station_锁付站.Function_打螺丝(2, true))
                                {
                                    n_执行步骤 = -99;
                                }
                                break;
                            }
                        case 10:
                            //结束
                            {
                                station_锁付站.StepEnd(true);
                                station_锁付站.Function_Z轴回安全位(true);
                                line.lineScrew.StopClyliner.Function_后退(true);
                                line.lineScrew.JackUpClyliner.Function_后退(true);
                                n_执行步骤 = -1;
                                break;
                            }
                    }
                });

            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
            }
            if (n_执行步骤 == -99)
            {
                lbl_Result.Text = $"{n_执行步骤}:NG";
                lbl_Result.BackColor = Color.Red;
                SetTestStep(int.MaxValue);
            }
            else
            {
                lbl_Result.Text = $"{n_执行步骤}:OK";
                lbl_Result.BackColor = Color.Green;
                SetTestStep(n_执行步骤 + 1);
            }
            station_锁付站.Function_Z轴回安全位(true);
            BtnEnable(true);
        }

        #region 废弃
        private async void btn_上CCDClib_Click(object sender, EventArgs e)
        {
            if (!UserDefineFunction.Check_AllAxis_回零())
                return;
            Station_锁付站 station_锁付站 = (Station_锁付站)StationMgr.GetInstance().GetStation(strStationName);
            BtnEnable(false);
            station_锁付站.Function_Z轴回安全位(true);
            try
            {
                await Task.Run(() =>
                {
                    station_锁付站.Clib_上相机九点标定(true);
                });
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
            }
            station_锁付站.Function_Z轴回安全位(true);
            BtnEnable(true);
        }

        private async void btn_下CCDClib_Click(object sender, EventArgs e)
        {
            if (!UserDefineFunction.Check_AllAxis_回零())
                return;
            Station_锁付站 station_锁付站 = (Station_锁付站)StationMgr.GetInstance().GetStation(strStationName);
            BtnEnable(false);
            station_锁付站.Function_Z轴回安全位(true);
            try
            {
                await Task.Run(() =>
                {
                    station_锁付站.Clib_下相机九点标定(true);
                });
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
            }
            station_锁付站.Function_Z轴回安全位(true);
            BtnEnable(true);
        }

        private async void btn_上CCD对位下CCD_Click(object sender, EventArgs e)
        {
            if (!UserDefineFunction.Check_AllAxis_回零())
                return;
            Station_锁付站 station_锁付站 = (Station_锁付站)StationMgr.GetInstance().GetStation(strStationName);
            BtnEnable(false);
            //station_锁付站.Function_Z轴回安全位(true);
            try
            {
                await Task.Run(() =>
                {
                    station_锁付站.Clib_上相机对位下相机(true);
                });
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
            }
            //station_锁付站.Function_Z轴回安全位(true);
            BtnEnable(true);
        }

        private async void btn_下CCD对位上CCD_Click(object sender, EventArgs e)
        {
            if (!UserDefineFunction.Check_AllAxis_回零())
                return;
            Station_锁付站 station_锁付站 = (Station_锁付站)StationMgr.GetInstance().GetStation(strStationName);
            BtnEnable(false);
            //station_锁付站.Function_Z轴回安全位(true);
            try
            {
                await Task.Run(() =>
                {
                    station_锁付站.Clib_下相机对位上相机(true);
                });
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
            }
            //station_锁付站.Function_Z轴回安全位(true);
            BtnEnable(true);
        }

        private async void btn_旋转Clib_Click(object sender, EventArgs e)
        {
            if (!UserDefineFunction.Check_AllAxis_回零())
                return;
            Station_锁付站 station_锁付站 = (Station_锁付站)StationMgr.GetInstance().GetStation(strStationName);
            BtnEnable(false);
            station_锁付站.Function_Z轴回安全位(true);
            try
            {
                await Task.Run(() =>
                {
                    station_锁付站.Clib_旋转标定(true);
                });
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
            }
            station_锁付站.Function_Z轴回安全位(true);
            BtnEnable(true);
        }

        private async void btn_夹爪拍照_Click(object sender, EventArgs e)
        {
            if (!UserDefineFunction.Check_AllAxis_回零())
                return;
            Station_锁付站 station_锁付站 = (Station_锁付站)StationMgr.GetInstance().GetStation(strStationName);
            BtnEnable(false);
            station_锁付站.Function_Z轴回安全位(true);
            try
            {
                await Task.Run(() =>
                {
                    station_锁付站.Calib_夹爪标定(true);
                });
            }
            catch (Exception EX)
            {
                MessageBox.Show(EX.Message);
            }
            station_锁付站.Function_Z轴回安全位(true);
            BtnEnable(true);
        }

        #endregion

        private void btn_旋转Clib(object sender, EventArgs e)
        {

        }
    }
}
